package day12.network;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class EchoClient {
    public void start(){
        try{
            Socket s = new Socket("127.0.0.1", 8000);
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(),"GBK"));
            // Printer 有println方法
            PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "GBK"));
            while (true){
                System.out.println("输入exit退出:");
                String str = new Scanner(System.in).nextLine();
                if("exit".equals(str)){
                    s.close();
                    break;
                }
                //把str发布到服务器
                out.println(str);
                out.flush();
                //接收回声数据
                String line = in.readLine();
                System.out.println("回声:"+line);
            }

        }catch (Exception e){
            System.out.println("已经与服务器断开连接");
            //e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EchoClient client = new EchoClient();
        client.start();
    }
}
